/*
** str_to_word_tab.c for soutien in /home/le_e//soutien
** 
** Made by canh-kiet le
** Login   <le_e@epitech.net>
** 
** Started on  Mon Feb 20 10:58:56 2012 canh-kiet le
** Last update Mon Apr 30 17:03:39 2012 canh-kiet le
*/

#include	"my.h"

static int     	my_strlen_plus(char *s, char spec_char)
{
  int		count;

  count = 0;
  if (s != NULL)
    while (s[count] != spec_char && s[count])
      count++;
  return (count);
}

static char    	*copy_word(char *s)
{
  char		*str;
  int		i;

  i = 0;
  str = xmalloc((my_strlen_plus(s, ' ') + 1) * sizeof(*str));
  while (*s != ' ' && *s)
    str[i++] = *(s++);
  str[i] = 0;
  return (str);
}

static int     	count_word(char *s)
{
  int		count;

  count = 0;
  if (s == NULL || *s == 0)
    return (0);
  while (*s)
    {
      if (*s == ' ')
	count++;
      s++;
    }
  return (count + 1);
}

char		**str_to_word_tab(char *s)
{
  char		**tab;
  int		nb;
  int		i;

  i = 0;
  epur_str(s);
  nb = count_word(s);
  tab = xmalloc((nb + 1) * sizeof(tab));
  while (*s)
    {
      if (*s != ' ')
	tab[i++] = copy_word(s);
      while (*s != ' ' && *s)
	s++;
      if (*s == ' ')
	s++;
    }
  tab[i] = NULL;
  return (tab);
}
